<template>
	<view>
		<!-- <u-navbar title="我的好友" :autoBack="true" @rightClick="toGroupDetail" :rightText="添加好友"></u-navbar> -->
		<u-navbar :is-back="true" title="我的好友" @leftClick="customBack" @rightClick="toNews"
			:rightText="rightText"></u-navbar>
		<view class="" style="height: 80px;">

		</view>
		<view class="center " v-for="item in lists" :key="item.id">
			<view class="d-flex bb" @tap="">
				<view class="flex-1 mt-3 mb-3 ml-2 fs28 ls2" style="display: flex;align-items: center;">
					<image v-if="item.member.icon" :src="item.member.icon" mode=""
						style="width:50px;height: 50px;margin-right: 15px;border-radius: 50px;"></image>
					<image v-else src="@/static/logo.png" mode=""
						style="width:50px;height: 50px;margin-right: 15px;border-radius: 50px;"></image>
					<span>{{item.member.usernumber}}({{item.member.realname}})</span>
				</view>
				<span style="color: red;font-size: 14px;" @tap="goChating(item.member.uid);return false;">去聊天</span>
			</view>

		</view>

		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				page: 1,
				totalCount: 0,
				lists: [],
				loadingType: 'more',
				rightText: '添加好友'
			}
		},
		onLoad() {

			if (!this.hasLogin) {
				this.$api.msg('尚未登录，请先登录');
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}, 1000)
				return false;
			}

			this.loadData()
			let _this = this;
			uni.$on('changeFriendsList', () => {
				_this.loadData('refresh')
			});
		},

		onPullDownRefresh() {
			this.loadingType = 'more'
			this.loadData('refresh')
		},

		//下拉到底获取新的数据
		onReachBottom() {
			if (!this.hasLogin) {
				this.islogin = true;
			} else {
				this.loadData()
			}
		},

		computed: {
			...mapState(['hasLogin']),
		},
		methods: {
			
			customBack() {
				uni.navigateBack({
					delta: 1
				})
			},

			toNews() {
				uni.navigateTo({
					url: '/pages/chat/news'
				})
			},

			async loadData(type = "add") {
				if (this.loadingType == 'nomore') {
					return false;
				}
				uni.showLoading({
					title: '加载中...'
				});

				let token = uni.getStorageSync('token');

				if (!token) {
					this.$api.msg('token缺失！');
				}
				let url = '/member_groups_list';
				let res = await this.$myRequest({
					url: url,
					methods: 'post',
					data: {
						token: token
					}
				})

				let ndata = res.data.data.lists;
				this.totalCount = res.data.data.total;

				if (this.page === 1) {
					this.lists = ndata;
					if (ndata.length > 0) {
						this.page = this.page + 1;
					}
				} else {
					if (ndata.length === 0) {
						if (this.lists.length === 0) {
							this.$api.msg('亲！暂无数据！！！')
						} else {
							this.$api.msg('亲！已经到底了！！！')
						}

					} else {
						this.lists = [...this.lists, ...ndata];
						this.page = this.page + 1;
					}
				}

				if (this.totalCount > this.lists.length) {
					this.loadingType = 'more'
				} else {
					this.loadingType = 'nomore'
				}

				uni.hideLoading();
				if (type == 'refresh') {
					uni.stopPullDownRefresh()
				}
			},

			goChating(id) {
				uni.navigateTo({
					url: '/pages/chat/chating?chatGroupId=' + id
				})
			}
		}
	}
</script>

<style>
	/* subpages2/text2/text2.wxss */
	page {
		background: #f2f2f2;
	}

	.card_main {
		padding: 0 20rpx;
		background: #ffffff;
		border-radius: 10rpx;
	}

	.center {
		width: 94%;
		height: 100%;
		background-color: white;
		margin: 20rpx auto;
		border-radius: 20rpx;
	}

	.bb {
		border: 1rpx solid #f3f3f3;
	}

	.d-flex {
		align-items: center;
	}

	.d-flex span {
		margin-right: 40upx;
	}
</style>